<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input style="width: 200px;" class="filter-item" placeholder="应用名称" v-model="listQuery.name">
      </el-input>
      <el-button class="filter-item" type="primary" v-waves icon="el-icon-search" @click="fetchData">查询</el-button>
      <router-link :to="'/inner/databasetask/edit'">
        <el-button class="filter-item"  type="primary" icon="el-icon-edit">新增</el-button>
      </router-link>
    </div>

    <el-table :data="tableData"  border fit highlight-current-row
              style="width: 100%">
      <el-table-column label="应用名称"  prop="name">
      </el-table-column>
      <el-table-column label="版本号" prop="version">
      </el-table-column>
      <el-table-column label="编码" prop="code">
      </el-table-column>
      <el-table-column label="创建时间" prop="createAt">
      </el-table-column>
      <el-table-column label="状态" >
        <template slot-scope="scope">
          <span>{{scope.row.status === 1 ? "正常" : "停用"}}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        show-overflow-tooltip
        width="350">
        <template slot-scope="scope">
          <router-link :to="'/inner/databasetask/edit?id='+scope.row.id">
            <el-button type="primary" size="small">编辑</el-button>
          </router-link>
          <span v-if="!scope.row.currentApp">
            <el-button type="success" v-if="scope.row.status === 1" @click="changeStatus(scope.row.id, 2)" size="small">停用</el-button>
            <el-button type="warning" v-if="scope.row.status === 2" @click="changeStatus(scope.row.id, 1)" size="small">启用</el-button>
            <el-button type="danger" size="small" @click="deleteApp(scope.row.id)">删除</el-button>
          </span>
        </template>
      </el-table-column>

    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageInfo.pageNum"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageInfo.total">
    </el-pagination>
  </div>

</template>

<script>
  import {getAppList, changeAppStatus, deleteApp} from '@/api/api'
  import waves from '@/directive/waves'
  export default {
    directives: {
      waves
    },
    data() {
      return {
        tableData: [],
        pageInfo: {
          pageNum: 1,
          pageSize: 10,
          total: 0
        },
        listQuery : {
          name : ''
        }
      }
    },

    methods: {
      fetchData() {
        let param = {
          pageNum : this.pageInfo.pageNum,
          pageSize : this.pageInfo.pageSize
        };
        if (this.listQuery.name && this.listQuery.name !== '' ) {
          param.name = this.listQuery.name;
        }
        getAppList(param).then((res) => {
          if (res.code === 200) {
            if (res.result) {
              this.tableData = res.result && res.result.list ? res.result.list : [];
              this.pageInfo.total = res.result.total;
            }
          } else {
            this.$message.error(res.message);
          }
        })
      },
      //更改状态
      changeStatus(id, status) {
        changeAppStatus({
          id : id,
          status : status
        }).then((res) => {
          if (res.code === 200) {
            this.$message.success("操作成功！");
            this.fetchData();
          } else {
            this.$message.error(res.message);
          }
        });
      },
      deleteApp(id) {
        deleteApp({id : id}).then((res) => {
          if (res.code === 200) {
            this.$message.success("操作成功！");
            this.fetchData();
          } else {
            this.$message.error(res.message);
          }
        })
      },
      // toEditPage(id) {
      //   this.$router.push({name:'/app/edit',query:{id:id}})
      // },
      handleSizeChange(val) {
        this.pageInfo.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.pageInfo.pageNum = val
        this.fetchData()
      },
    },
    created() {
      this.fetchData()
    },
  }
</script>
